/*
    Javascript functions used by user signup, login, and profile page
*/

function ChangePage(obj){
    if(obj.id == 'next1' && ValidateEmail() && CheckPassword()){
        $('page1').style.display = 'none';
        $('page2').style.display = 'inline';
    }
    else if(obj.id == 'back2'){
        $('page2').style.display = 'none';
        $('page1').style.display = 'inline';
    }
    else if(obj.id == 'next2' && PageTwoCheckForm()){
        $('page2').style.display = 'none';
        $('page3').style.display = 'inline';
    }
    else if(obj.id == 'back3'){
        $('page3').style.display = 'none';
        $('page2').style.display = 'inline';
    }
    else if(obj.id == 'next3'){
        $('page3').style.display = 'none';
        $('page4').style.display = 'inline';
    }
    else if(obj.id == 'back4'){
        $('page4').style.display = 'none';
        $('page3').style.display = 'inline';
    }
}

function PageTwoCheckForm(){
    var inputs = $('page2').getElementsByTagName('input');
    var time = new Date();
    var year = time.getYear();
    var month = time.getMonth() + 1;
    
    if(year < 2000){
        year = year + 1900;
    }

    for (var i = 0; i < inputs.length; i++){
        if(!inputs[i].value && inputs[i].className){
            alert(inputs[i].className);
            return false;
        }
        else if(inputs[i].id == "bmonth" && (inputs[i].value < 1 || inputs[i].value > 12)){
            alert('Please enter a valid birthday month');
            return false;
        }
        else if(inputs[i].id == "bday" && (inputs[i].value < 1 || inputs[i].value > 31)){
            alert('Please enter a valid birthday day');
            return false;
        }
        else if (inputs[i].id == "byear" && (inputs[i].value < 1900 || inputs[i].value > year)){
            alert('Please enter a valid birthday year');
            return false;
        }
    }
    
    if($('byear').value == year && ($('bmonth').value > month || ($('bmonth').value == month && $('bday').value > time.getDate()))){
        alert('That birthday is in the future.  Please enter a valid birthday');
        return false;
    }
    
    return true;
}

function ValidateEmail(){
    emailregex = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
    
    if(!$('email').value){
        alert('Please enter an email');
        return false;
    }
    else if(!emailregex.test($('email').value)){
        alert('Please enter a valid email');
        return false;
    }
    else {
        var req = new Request({
                method: 'post',
                url: 'signup/checkemail',
                data: { 'email' : $('email').value },
                async: false,
                onRequest: '',
                onComplete: function(response) { 
                    var data = JSON.decode(response);
                    if(data){
                        $('email').valid = 0;
                    }
                    else {
                        $('email').valid = 1;
                    }
                }
        }).send();
        
        if(!$('email').valid){
            alert('That email is already registered, please choose another');
            return false;
        }
        else {
            return true;
        }
    }
}

function CheckPassword(){
    if(!$('password').value){
        alert('Please enter a password');
        return false;
    }
    else if (!$('confirmpassword').value){
        alert('Please confirm your password');
        return false;
    }
    else if($('password').value == $('confirmpassword').value){
        return true;
    }
    else {
        alert("Your passwords don't match");
        return false;
    }
}

function AddSchool(){
    var schools = document.getElementsByName('schoolname');
    var scount = schools.length;
    var newscount = schools.length + 1
    var rownumber = scount + $('schoolfirstrow').rowIndex;
    var newscount = scount + 1;
    var rownumber = (scount * 4) + $('schoolfirstrow').rowIndex;
    if(scount > 1){
        rownumber = rownumber + (scount - 1);
    }
    
    var newrow = $('page4table').insertRow(rownumber);
    var cellone = newrow.insertCell(0);
    var celltwo = newrow.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = "&nbsp;";
    
    var newrow2 = $('page4table').insertRow(rownumber + 1);
    var cellone = newrow2.insertCell(0);
    var celltwo = newrow2.insertCell(1);
    cellone.innerHTML = "School Name:";
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<input tye="text" id="schoolname'
                    + newscount + '1" name="schoolname">';
    
    var newrow3 = $('page4table').insertRow(rownumber + 2);
    var cellone = newrow3.insertCell(0);
    cellone.innerHTML = "Graduation Year:";
    var celltwo = newrow3.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<input type="text" id="schoolyear'
                    + newscount + '" name="schoolyear" maxlength="4" size="4">';
    
    var newrow4 = $('page4table').insertRow(rownumber + 3);
    var cellone = newrow4.insertCell(0);
    cellone.innerHTML = "School Type:";
    var celltwo = newrow4.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<select id="schooltype' + newscount + '" name="schooltype">'
                        + '<option value="undergraduate">Undergraduate College</option>'
                        + '<option value="highschool">High School</option>'
                        + '<option value="graduate">Graduate College</option>'
                    + newscount + '</select>';
    
    var newrow5 = $('page4table').insertRow(rownumber + 4);
    var cellone = newrow5.insertCell(0);
    cellone.innerHTML = "Concentration:";
    var celltwo = newrow5.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<input type="text" id="schoolconcentration'
                    + newscount + '" name="schoolconcentration">';
}

function AddScreenname(){
    var screennames = document.getElementsByName('screenname');
    var sncount = screennames.length;
    var rownumber = sncount + $('screennamefirstrow').rowIndex;
    var newrow = $('page4table').insertRow(rownumber);
    var cellone = newrow.insertCell(0);
    var celltwo = newrow.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = "<input type='text' id='screenname"
        + (sncount + 1) + "' name='screenname'>&nbsp;"
        + "<select id='screennametype"
        + (sncount + 1) + "' name='screennametype'>"
        + "<option value='aim'>AIM</option><option value='yahoo'>Yahoo</option>"
        + "<option value='msn'>MSN</option><option value='skype'>Skype</option>"
        + "</select>";
}

function AddEmployer(){
    var employers = document.getElementsByName('employername');
    var empcount = employers.length;
    var newempcount = empcount + 1;
    var rownumber = (empcount * 8) + $('employerfirstrow').rowIndex;
    if(empcount > 1){
        rownumber = rownumber + (empcount - 1);
    }
    var newrow = $('page4table').insertRow(rownumber);
    var cellone = newrow.insertCell(0);
    var celltwo = newrow.insertCell(1);
    cellone.innerHTML = "&nbsp;";
    
    var newrow2 = $('page4table').insertRow(rownumber + 1);
    var cellone = newrow2.insertCell(0);
    var celltwo = newrow2.insertCell(1);
    cellone.innerHTML = "Employer Name:";
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = "<input type='text' id='employername" + newempcount + "' name='employername'>";
    
    var newrow3 = $('page4table').insertRow(rownumber + 2);
    var cellone = newrow3.insertCell(0);
    cellone.innerHTML = "Position:";
    var celltwo = newrow3.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<input type="text" id="employerposition' + newempcount + '" name="employerposition">';
    
    var newrow4 = $('page4table').insertRow(rownumber + 3);
    var cellone = newrow4.insertCell(0);
    cellone.innerHTML = "Status:";
    var celltwo = newrow4.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<input type="checkbox" id="employerstatus' + newempcount + '" name="employerstatus" defaultChecked="false" onclick="StatusChange(this, ' + newempcount + ')">I presently work here';
    
    var newrow5 = $('page4table').insertRow(rownumber + 4);
    var cellone = newrow5.insertCell(0);
    cellone.innerHTML = "Time Period:";
    var celltwo = newrow5.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<input type="text" id="empmonth' + newempcount +'" name="empmonth" size="2" maxlength="2">'
            + '-<input type="text" id="empyear' + newempcount + '" name="empyear" size="4" maxlength="4"> (mm-yyyy) to '
            + '<span id="empdate' + newempcount + '">'
                + '<input type="text" id="emplastmonth' + newempcount + '" name="emplastmonth" size="2" maxlength="2">'
            + '-<input type="text" id="emplastyear' + newempcount + '" name="emplastyear" size="4" maxlength="4">'
            + '</span>';
    
    var newrow6 = $('page4table').insertRow(rownumber + 5);
    var cellone = newrow6.insertCell(0);
    cellone.innerHTML = "Address:";
    var celltwo = newrow6.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<input type="text" id="employeraddress' + newempcount + '" name="employeraddress">';
    
    var newrow7 = $('page4table').insertRow(rownumber + 6);
    var cellone = newrow7.insertCell(0);
    cellone.innerHTML = "City:";
    var celltwo = newrow7.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<input type="text" id="employercity' + newempcount + '" name="employercity">';
    
    var newrow8 = $('page4table').insertRow(rownumber + 7);
    var cellone = newrow8.insertCell(0);
    cellone.innerHTML = "State:";
    var celltwo = newrow8.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<select id="employerstate'
            + newempcount + '" name="employerstate">' + Statedropdown()
            + '</select>';
    
    var newrow9 = $('page4table').insertRow(rownumber + 8);
    var cellone = newrow9.insertCell(0);
    cellone.innerHTML = "Zip:";
    var celltwo = newrow9.insertCell(1);
    celltwo.className = "secformcolumn";
    celltwo.innerHTML = '<input type="text" id="employerzip' + newempcount + '" name="employerzip" maxlength="5" size="5">';
}

function StatusChange(obj, num){
    var empdate = 'empdate' + num;
    if(obj.checked){
        $(empdate).innerHTML = "present";   
    }
    else {
        $(empdate).innerHTML = '<input type="text" id="emplastmonth'
        + num + '" name="emplastmonth" size="2" maxlength="2">-'
        + '<input type="text" id="emplastyear'
        + num + '" name="emplastyear" size="4" maxlength="4">';
    }
}

function Statedropdown(){
    return "<option value='AL'>Alabama</option>" +
        "<option value='AK'>Alaska</option>" +
        "<option value='AZ'>Arizona</option>" +
        "<option value='AR'>Arkansas</option>" +
        "<option value='CA'>California</option>" +
        "<option value='CO'>Colorado</option>" +
        "<option value='CT'>Connecticut</option>" +
        "<option value='DE'>Delaware</option>" +
        "<option value='DC'>Dist of Columbia</option>" +
        "<option value='FL'>Florida</option>" +
        "<option value='GA'>Georgia</option>" +
        "<option value='HI'>Hawaii</option>" +
        "<option value='ID'>Idaho</option>" +
        "<option value='IL'>Illinois</option>" +
        "<option value='IN'>Indiana</option>" +
        "<option value='IA'>Iowa</option>" +
        "<option value='KS'>Kansas</option>" +
        "<option value='KY'>Kentucky</option>" +
        "<option value='LA'>Louisiana</option>" +
        "<option value='ME'>Maine</option>" +
        "<option value='MD'>Maryland</option>" +
        "<option value='MA'>Massachusetts</option>" +
        "<option value='MI'>Michigan</option>" +
        "<option value='MN'>Minnesota</option>" +
        "<option value='MS'>Mississippi</option>" +
        "<option value='MO'>Missouri</option>" +
        "<option value='MT'>Montana</option>" +
        "<option value='NE'>Nebraska</option>" +
        "<option value='NV'>Nevada</option>" +
        "<option value='NH'>New Hampshire</option>" +
        "<option value='NJ'>New Jersey</option>" +
        "<option value='NM'>New Mexico</option>" +
        "<option value='NY'>New York</option>" +
        "<option value='NC'>North Carolina</option>" +
        "<option value='ND'>North Dakota</option>" +
        "<option value='OH'>Ohio</option>" +
        "<option value='OK'>Oklahoma</option>" +
        "<option value='OR'>Oregon</option>" +
        "<option value='PA'>Pennsylvania</option>" +
        "<option value='RI'>Rhode Island</option>" +
        "<option value='SC'>South Carolina</option>" +
        "<option value='SD'>South Dakota</option>" +
        "<option value='TN'>Tennessee</option>" +
        "<option value='TX'>Texas</option>" +
        "<option value='UT'>Utah</option>" +
        "<option value='VT'>Vermont</option>" +
        "<option value='VA'>Virginia</option>" +
        "<option value='WA'>Washington</option>" +
        "<option value='WV'>West Virginia</option>" +
        "<option value='WI'>Wisconsin</option>" +
        "<option value='WY'>Wyoming</option>";
}